package com.five.welcome.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.five.welcome.entity.Orders;
import com.five.welcome.entity.statistic.PayStatusCount;
import com.five.welcome.result.Result;
import com.five.welcome.service.OrdersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ywt
 * @since 2024-07-12
 */
@RestController
@CrossOrigin
@Tag(name = "订单信息接口")
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    //根据学号查询订单
    @Operation(summary = "根据学号查询订单")
    @GetMapping("/getOrderBySNumber")
    public Result getOrderBySNumber(String sNumber) {
        Orders order = ordersService.getOrderBySNumber(sNumber);
        List<Orders> orders = new ArrayList<>();
        orders.add(order);
        return Result.ok(orders);
    }

    //根据订单号查询订单
    @Operation(summary = "根据订单号查询订单")
    @GetMapping("/getOrderByOrderId")
    public Result getOrderByOrderId(String orderId) {
        Orders order = ordersService.getOrderByOrderId(orderId);
        return Result.ok(order);
    }

    /**
     * 查询所有
     */
    @Operation(summary = "查询所有")
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Orders> list = ordersService.selectAll();
        return Result.ok(list);
    }

    /**
     * 分页条件查询所有
     */
    @Operation(summary = "分页条件查询所有")
    @GetMapping("/pageSelectAll")
    public Result pageSelectAll(@RequestParam(defaultValue = "1") Integer pageNumber,
                                @RequestParam(defaultValue = "2") Integer pageSize,
                                String sNumber, String payStatus) {
        Page<Orders> ordersPage = ordersService.pageSelectAll(pageNumber, pageSize, sNumber, payStatus);
        return Result.ok(ordersPage);
    }


    /**
     * 根据学号查询是否有订单
     */
    @Operation(summary = "根据学号查询是否有订单")
    @GetMapping("/getIsOrderBySNumber")
    public Result getIsOrderBySNumber(String sNumber) {
        List<Orders> list = List.of(ordersService.getIsOrderBySNumber(sNumber));
        return Result.ok(list);
    }

    //支付状态各自人数
    @Operation(summary = "支付状态各自人数")
    @GetMapping("/PayStatusCount")
    public Result PayStatusCount() {
        List<PayStatusCount> end = ordersService.PayStatusCount();
        return end.isEmpty() ? Result.error("数据丢失") : Result.ok(end);
    }
}

